
import { ref, reactive } from 'vue';
import dayjs from "dayjs";
import { settleStatusOptions, paymentTypeOptions } from "@/views/constant";
import { comfirmModal } from "@/hooks/useModal";
import { message } from "ant-design-vue";
import { useDict } from "@/hooks/useDict";
export const useTable = ({ getMoreData, deleteApi, exportApi }) => {
    const { regional_province, pay_type } = useDict([

        {
            key: 'regional_province',
        },
        {
            key: 'pay_type',
        },
    ]);
    const queryData = reactive({});
    const tableRef: any = ref(null);
    const dateVisible = ref(false);
    const deadline: any = ref(null);
    const queryOptions = ref([
        {
            name: "合同号",
            type: "input",
            value: "",
            placeholder: "请输入",
            filed: "contractNo",
        },
        {
            name: "终端用户",
            type: "input",
            value: "",
            placeholder: "请输入",
            filed: "terminalUser",
        },
        {
            name: "状态",
            type: "select",
            value: undefined,
            placeholder: "请选择",
            filed: "status",
            list: settleStatusOptions,
        },
        {
            name: "地域省份",
            type: "select",
            value: undefined,
            placeholder: "请选择",
            filed: "name",
            list: regional_province
            // async: () => {
            //     return new Promise((resolve) => {
            //         let list: any = [];
            //         setTimeout(() => {
            //             list = Array.from({ length: 11 }, () => ({
            //                 label: "2121",
            //                 value: new Date().getTime() + (Math.random() * 1000).toString(),
            //             }));
            //             resolve(list);
            //         }, 0);
            //     });
            // },
        },
        {
            name: "付款类型",
            type: "select",
            value: undefined,
            placeholder: "请选择",
            filed: "name",
            // list: paymentTypeOptions,
            list: pay_type
        },
        {
            name: "合同金额",
            type: "input",
            value: "",
            placeholder: "请输入",
            filed: "contractAmount",
        },
    ]);
    const onQueryDataByParams = (params) => {
        for (let key in params) {
            queryData[key] = params[key];
        }
        tableRef.value.refreshTableData();
    };

    // 根据日期查询逾期日期
    const changeDate = (date) => {
        const ids = tableRef.value.data?.map((item) => item.id);
        deadline.value = dayjs(date).format("YYYY-MM-DD");
        getMoreData({ ids, deadline: deadline.value }).then(
            (res) => {
                tableRef.value.changeData((data) => {
                    return data.map((item) => {
                        const obj = res.data.find((ele) => ele.id == item.id);
                        return {
                            ...item,
                            ...obj,
                        };
                    });
                });
                dateVisible.value = false;
            }
        );
    };



    // 删除记录
    const remove = (id) => {
        comfirmModal({
            content: "确认删除该记录吗？",
            onOk() {
                deleteApi({ id }).then(() => {
                    message.success("删除成功");
                    tableRef.value.fetch();
                });
            },
        });
    };
    // 导入
    const importData = (data) => {
        if (data) {
            const { errorList, successList } = data?.result;
            if (errorList?.length) {
                message.error(errorList[0].errMsg);
            }

            if (successList?.length) {
                message.success("导入成功");
                tableRef.value.refreshTableData();
            }
        }
    };
    // 导出
    const exportData = () => {
        if (!tableRef.value.data?.length) {
            message.error('暂无数据')
            return
        }
        const params = {}

        for (let key in queryData) {
            params[key] = queryData[key];
        }
        exportApi(params).then(() => {
            message.success("导出成功");
        });
    };

    const search = (params) => {
        deadline.value = "";
        onQueryDataByParams(params);
    };


    return {
        queryData,
        tableRef,
        onQueryDataByParams,
        importData,
        exportData,
        changeDate,
        search,
        dateVisible,
        queryOptions,
        remove
    }

}